import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router';
import { Http } from '@angular/http';
import 'rxjs/Rx';
import { Observable } from 'rxjs/Observable';

@Component({
  selector: 'app-admin-page',
  templateUrl: './admin-page.component.html',
  styleUrls: ['./admin-page.component.scss'],
  providers: []
})
export class AdminPageComponent implements OnInit {

  users: User[] = [];

  dataSource: Observable<any>;

  constructor(private router: Router, public http: Http) {
    this.dataSource = this.http.get('/api/user/get_users').map(response => response.json());
  }

  ngOnInit() {
    // To be implemented by http service
    this.dataSource.subscribe(
      data => {
        data.forEach(item => {
          if(item.active == 1){
            item.active_flag = '有效'
          }else{
            item.active_flag = '无效'
          }
        });
        this.users = data
        console.log(data);
      }
    )
  }

  editUser(user: User){
    // console.log(user.user_name);
    this.router.navigateByUrl('/admin/edit/' + user.user_name);
  }

  newUser(){
    this.router.navigateByUrl('/admin/newUser');
  }
}

export interface User{
  user_name: string;
  user_realname: string;
  user_password: string;
  active_flag: boolean;
}
